﻿using OpenQA.Selenium.Chrome;
using OpenQA.Selenium;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenQA.Selenium.DevTools;
using Reborn.Repository;
using SqlSugar;
using SimpleSpider.Framework;
using Reborn.Infrastructure;
using Newtonsoft.Json.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Net;

namespace SimpleSpider
{
    public partial class MainFrm : Form
    {
        public MainFrm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {


            try
            {
                // 设置Chrome选项，可选
                var options = new ChromeOptions();
                //options.AddArgument("--headless"); // 无界面模式


                options.AddArgument("--disable-blink-features=AutomationControlled"); //关闭提示受控制

                ChromeDriver driver = new ChromeDriver("chromedriver-win64",options);


                DevToolsSession session = driver.GetDevToolsSession();
                var network = session.GetVersionSpecificDomains<OpenQA.Selenium.DevTools.V122.DevToolsSessionDomains>().Network;

                //network.Enable(new EnableCommandSettings());
                //network.SetRequestInterception(new SetRequestInterceptionCommandSettings()
                //{
                //    Patterns = new RequestPattern[] { new RequestPattern { ResourceType = ResourceType.XHR } }
                //});

                //network.RequestIntercepted += Network_RequestIntercepted;
                //network.ResponseReceived += Network_ResponseReceived;
                //network.inte
                driver.Navigate().GoToUrl("http://www.douyin.com");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                // 关闭浏览器
               // driver.Quit();
            }
        }

        private void MainFrm_Load(object sender, EventArgs e)
        {
            Logger.Information("开始启动");
            try
            {
                App.Init();
            }
            catch (Exception ex)
            {


            }

        }

        private void 开始网址ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StartUrlFrm startUrlFrm = new StartUrlFrm();
            startUrlFrm.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                EasyCrawlUrlWorker easyCrawlUrlWorker  = new EasyCrawlUrlWorker();
                easyCrawlUrlWorker.Start();
            }
            catch (Exception ex)
            { 
            
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            EasyCrawlContentWorker easyCrawlContentWorker = new EasyCrawlContentWorker();
            easyCrawlContentWorker.Start();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                JToken jToken = JObject.Parse(textBox1.Text);
                JToken dataToken = jToken["data"]["results"][0];
                //用c#写一个方法

                string email = dataToken["contact"]["email"].ToString();
                string tel = dataToken["contact"]["tel"].ToString();
                string number = dataToken["number"].ToString();
                string major = dataToken["major"].ToString();
                string majorTerm = dataToken["major_term"].ToString();
                string gender = dataToken["gender"].ToString();
                string lawFirm = dataToken["lawFirm"].ToString();
                string provinceTerm = dataToken["province_term"].ToString();
                string lawyerName = dataToken["lawyerName"].ToString();

            }
            catch (Exception ex)
            {

            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string text = @"
            string email = dataToken[""contact""][""email""].ToString();
            string tel = dataToken[""contact""][""tel""].ToString();
            string number = dataToken[""number""].ToString();
            string major = dataToken[""major""].ToString();
            string majorTerm = dataToken[""major_term""].ToString();
            string gender = dataToken[""gender""].ToString();
            string lawFirm = dataToken[""lawFirm""].ToString();
            string provinceTerm = dataToken[""province_term""].ToString();
            string lawyerName = dataToken[""lawyerName""].ToString();";

            string pattern = @"string\s+(\w+)\s+=\s+dataToken\[.*?\].ToString\(\);";

            MatchCollection matches = Regex.Matches(text, pattern);

            List<string> propertyNames = new List<string>();

            foreach (Match match in matches)
            {
                string propertyName = match.Groups[1].Value;
                propertyNames.Add(propertyName);
            }

            // Generate entity class
            Console.WriteLine("public class YourEntityClass");
            Console.WriteLine("{");
            foreach (var propertyName in propertyNames)
            {
                Console.WriteLine($"    public string {propertyName} {{ get; set; }}");
            }
            Console.WriteLine("}");
        }

        class PropertyInfo
        {
            public string VariableName { get; set; }
            public string FirstKey { get; set; }
            public string SecondKey { get; set; }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                string oUrl = "https://lvmou.so.com/v1/law/lawyersearch?province={省}&city={城市}&p={分页}&sort=-score_term";

                string c = textBox2.Text;
                string[] line = textBox3.Lines;

                textBox4.Text = string.Empty;

                foreach (var item in line)
                {
                    if (string.IsNullOrEmpty(item)) continue;

                    string url = oUrl.Replace("{省}", c.Trim()).Replace("{城市}", item.Trim());
                    textBox4.AppendText(url + "\r\n");

                }
            }
            catch (Exception ex)
            { 
            
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            try
            {

                    WebClient webClient = new WebClient();
                    string s = webClient.DownloadString("https://sch.shanchendaili.com/api.html?action=get_ip&key=HU97a770213977759925w62e&time=1&count=1&type=json&only=1");

                JToken dailiToken = JObject.Parse(s);
                string ip = dailiToken["list"][0]["sever"].ToString();
                string port = dailiToken["list"][0]["port"].ToString();
            }
            catch (Exception ex)
            { 
            
            }
        }
    }

    [SugarTable("tests")]
    public class TestsTable
    {
        /// <summary>
        /// id
        /// </summary>
        [SugarColumn(IsNullable = false, IsPrimaryKey = true, IsIdentity = true, ColumnName = "id")]

        public int Id { get; set; }

        /// <summary>
        /// 名称
        /// </summary>
        [SugarColumn(ColumnName = "name", ColumnDescription = "名称")]
        public string Name { get; set; }

    }
}
